package com.czk.netty.nio.basic;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * channel的示例
 *
 * @Author:ChenZhangKun
 * @Date: 2021/1/10 22:57
 */
public class ChannelTest1 {
    public static void main(String[] args) throws IOException {
        String str = "czk";
        // 创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
        // 拿到通道
        FileChannel channel = fileOutputStream.getChannel();
        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 缓冲区放入数据
        byteBuffer.put(str.getBytes());
        // 写入文件
        byteBuffer.flip();
        channel.write(byteBuffer);
        byteBuffer.clear();
    }
}
